package com.servicesystem.servicesystem.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.servicesystem.servicesystem.entity.douyin.GoodsLib;
import com.servicesystem.servicesystem.entity.douyin.vo.goodsLib.GoodsLibVO;
import com.servicesystem.servicesystem.entity.douyin.vo.oauth.OauthVO;
import com.servicesystem.servicesystem.entity.douyin.vo.product.AddProductVO;
import com.servicesystem.servicesystem.entity.douyin.vo.product.AddSpecsVO;
import com.servicesystem.servicesystem.mapper.GoodsLibMapper;
import com.servicesystem.servicesystem.service.GoodsLibService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.servicesystem.servicesystem.untis.Http;
import com.servicesystem.servicesystem.untis.HttpRequestAll;
import com.servicesystem.servicesystem.untis.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author X
 * @since 2020-09-12
 */
@Service
public class GoodsLibServiceImpl extends ServiceImpl<GoodsLibMapper, GoodsLib> implements GoodsLibService {

    /***
     * 获取1688商品列表
     */
    @Override
    public List<GoodsLib> getGoodsLib() {
        List<GoodsLib> goodsLibs = baseMapper.selectList(null);
        return goodsLibs;
    }


    @Override
    public String getGoodsLibProductId(String id) {
        QueryWrapper<GoodsLib> goodsLibQueryWrapper = new QueryWrapper<>();
        goodsLibQueryWrapper.eq("id",id);
        GoodsLib goodsLib = baseMapper.selectOne(goodsLibQueryWrapper);
        String productId = goodsLib.getProductId();
        return productId;
    }

    /***
     * 通过id查询商品数据
     * @param id
     * @return
     */
    @Override
    public GoodsLib getGoodsInfoById(String id) {
        QueryWrapper<GoodsLib> goodsLibQueryWrapper = new QueryWrapper<>();
        goodsLibQueryWrapper.eq("id",id);
        GoodsLib goodsLib = baseMapper.selectOne(goodsLibQueryWrapper);
        return goodsLib;
    }
    /*@Override
    public List<GoodsLibVO> getGoodsLib() {
        List<GoodsLib> goodsLibs = baseMapper.selectList(null);
        ArrayList<GoodsLibVO> goodsLibVOS = new ArrayList<>();

        for (GoodsLib goodsLib : goodsLibs) {
            GoodsLibVO goodsLibVO = new GoodsLibVO();

            String skuInfos = goodsLib.getSkuInfos();
            goodsLibVO.setSkuInfos(skuInfos);
            //System.out.println("skuInfos..." + skuInfos);

            List skuInfosList = JSON.parseObject(skuInfos, List.class);
            //遍历skuInfo
            for (Object skuInfosObj : skuInfosList) {
                String skuInofsObjStr = JSON.toJSONString(skuInfosObj);
                //System.out.println("skuInofsObjStr..."+skuInofsObjStr);
                Map skuInofsObjStrMap = JSON.parseObject(skuInofsObjStr, Map.class);

                Object attributes = skuInofsObjStrMap.get("attributes");
                String attributesStr = JSON.toJSONString(attributes);
                //System.out.println("attributesStr..."+attributesStr);
                List attributesStrList = JSON.parseObject(attributesStr, List.class);
                for (Object attributesStrListObj : attributesStrList) {
                    String attributesStrListObjStr = JSON.toJSONString(attributesStrListObj);
                    System.out.println("attributesStrListObjStr..."+attributesStrListObjStr);
                    Map map = JSON.parseObject(attributesStrListObjStr, Map.class);
                    Object attributeValue = map.get("attributeValue");
                    String attributeValueStr = JSON.toJSONString(attributeValue);

                    //商品图片
                    String image = goodsLib.getImage();
                    Map imageMap = JSON.parseObject(image, Map.class);
                    Object images = imageMap.get("images");
                    String imagesStr = JSON.toJSONString(images);
                    List imagesList = JSON.parseObject(imagesStr, List.class);
                    goodsLibVO.setPic(imagesList);

                    String description = goodsLib.getDescription();
                    List descriptionList = JSON.parseObject(description, List.class);
                    goodsLibVO.setDescription(descriptionList);
                    String subject = goodsLib.getSubject();
                    //System.out.println("subject..." + subject);
                    goodsLibVO.setOutProductName(subject+attributeValueStr);

                    //商品价格
                    Object price = skuInofsObjStrMap.get("price");
                    String priceStr = JSON.toJSONString(price);
                    goodsLibVO.setDiscountPrice(priceStr);
                    Object skuId = skuInofsObjStrMap.get("skuId");
                    String skuIdStr = JSON.toJSONString(skuId);
                    goodsLibVO.setOutProductId(skuIdStr);
                    goodsLibVOS.add(goodsLibVO);
                }

                //System.out.println("attributesStr..."+attributesStr);
                goodsLibVO.setSkuInfos(skuInfos);
            }*/


            /*List skuInfosList = JSON.parseObject(skuInfos, List.class);
            for (Object skuInfosObj : skuInfosList) {
                String skuInofsObjStr = JSON.toJSONString(skuInfosObj);
                goodsLibVO.setSkuInofsObjStr(skuInofsObjStr);
                goodsLibVOS.add(goodsLibVO);
                Map skuInofsObjStrMap = JSON.parseObject(skuInofsObjStr, Map.class);


                Object attributes = skuInofsObjStrMap.get("attributes");
                String attributesStr = JSON.toJSONString(attributes);
                List attributesStrList = JSON.parseObject(attributesStr, List.class);


                for (Object attributesStrListObj : attributesStrList) {
                    String attributesStrListObjStr = JSON.toJSONString(attributesStrListObj);
                    Map attributesStrListObjMap = JSON.parseObject(attributesStrListObjStr, Map.class);
                    Object attributeValue = attributesStrListObjMap.get("attributeValue");
                    String attributeValueStr = JSON.toJSONString(attributeValue);

                    goodsLibVO.setOutProductName(subject + attributeValueStr);
                    System.out.println("subject + attributeValueStr..." + subject + attributeValueStr);

                    Object price = skuInofsObjStrMap.get("price");
                    String priceStr = JSON.toJSONString(price);
                    goodsLibVO.setDiscountPrice(priceStr);
                    Object skuId = skuInofsObjStrMap.get("skuId");
                    String skuIdStr = JSON.toJSONString(skuId);
                    goodsLibVO.setOutProductId(skuIdStr);

                    System.out.println("goodsLibVO..." + goodsLibVO);
                }
                System.out.println("skuInofsObjStr..." + skuInofsObjStr);
            }*/
       /* }
        return goodsLibVOS;
    }*/

}
